﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using KC.DataAccess.Global;
using enc = KC.Business.Text.Encoding;

public partial class _Default : System.Web.UI.Page
{
	private static string cs = System.Configuration.ConfigurationManager.ConnectionStrings["MV8"].ConnectionString;
	private int page = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
		// Accept specific post requests and redirect accordingly
		if (Request.QueryString["Req"] != null && Request.QueryString["PID"] != null) {
			int PID = -1;
			if (int.TryParse(Request.QueryString["PID"], out PID)) {
				DataTable dtp = KC.DataAccess.Global.Global.CallSql_Query(cs, "select friendlyname from posts where id=" +PID.ToString());
				Response.RedirectPermanent("http://www.kconnolly.net/Post.aspx?Title=" + dtp.Rows[0][0].ToString()); }
			return; }
		else if (Request.QueryString["Req"] != null && Request.QueryString["Req"].ToLower() == "post" && Request.QueryString["ID"] != null) {
			int PID = -1;
			if (int.TryParse(Request.QueryString["ID"], out PID)) {
				DataTable dtp = KC.DataAccess.Global.Global.CallSql_Query(cs, "select friendlyname from posts where id=" +PID.ToString());
				Response.RedirectPermanent("http://www.kconnolly.net/Post.aspx?Title=" + dtp.Rows[0][0].ToString()); }
			return; }
		else if (Request.QueryString["Req"] != null && Request.QueryString["Req"].ToLower() == "post" && Request.QueryString["Title"] != null) {
			Response.RedirectPermanent("http://www.kconnolly.net/Post.aspx?Title=" + Request.QueryString["Title"]);
			return; }

		// Look up and databind recent posts
		if (Request.QueryString["Page"] != null)
			int.TryParse(Request.QueryString["Page"], out page);

		// Paginate
		int skip = (10 * page + 1);
		lblPage.Text = (page + 1).ToString();
		hlOlder.Text = "&lt; Older: Page " + (page + 2).ToString();
		hlOlder.NavigateUrl = ".?Page=" + (page + 1).ToString();
		hlNewer.NavigateUrl = ".?Page=" + (page - 1).ToString();
		hlNewer.Text = "Newer: Page " + page.ToString() + " &gt;";
		hlNewer.Enabled = page > 0;

		// Bind posts
		DataTable dtPosts = Global.CallSql_Query(cs, "with Posts2 as (select Title, Body, Keywords, FriendlyName, Created, isenabled, row_number() over (order by id desc) as RowNumber from Posts) select top 10 Title, Body, Keywords, FriendlyName, Created " +
			"from Posts2 where RowNumber between " + skip.ToString() + " and " + (skip + 10).ToString() + " and isenabled=1");
		rPosts.DataSource = dtPosts;
		rPosts.DataBind();
    }
	protected void rPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
		DataRowView dr = (DataRowView)e.Item.DataItem;
		string url = "http://www.kconnolly.net/Post.aspx?Title=" + (string)dr["FriendlyName"];

		if (dr["Keywords"].GetType() != typeof(DBNull) && ((string)dr["Keywords"]).Trim().Length > 0 && e.Item.ItemIndex == 0)
			foreach (Control c in Header.Controls)
				if (c.GetType() == typeof(System.Web.UI.HtmlControls.HtmlMeta) && ((System.Web.UI.HtmlControls.HtmlMeta)c).Name == "Keywords") {
					Header.Controls.Remove(c);
					Header.Controls.Add(new System.Web.UI.HtmlControls.HtmlMeta() { Name = "Keywords", Content = (string)dr["Keywords"] }); 
					break; }

		// Hyperlink stuff
		{	HyperLink hlTitle = (HyperLink)e.Item.FindControl("hlTitle");
			hlTitle.Text = (string)dr["Title"];
			hlTitle.ToolTip = (string)dr["Title"];
			hlTitle.NavigateUrl = url; }

		// Labels
		((Label)e.Item.FindControl("lblDate")).Text = ((DateTime)dr["Created"]).ToString("MM/dd/yyyy hh:mm ttt");
		Label lblBody = ((Label)e.Item.FindControl("lblBody"));
		lblBody.Text = enc.StripTagsCharArray(enc.BlogDecodeForRendering((string)dr["Body"])).Replace("[br]", "<br />");

		while (lblBody.Text.Contains("<br /><br />")) lblBody.Text = lblBody.Text.Replace("<br /><br />", "<br />");
		while (lblBody.Text.Contains("  ")) lblBody.Text=lblBody.Text.Replace("  "," ");

		if (lblBody.Text.Length > 1024)
			lblBody.Text = lblBody.Text.Substring(0, 1024) + "[...]";
		((Label)e.Item.FindControl("lblFacebook")).Text = site.GetFacebookLikeButton(url);
			
		if (e.Item.ItemIndex == 2 || e.Item.ItemIndex == 4)
			((Label)e.Item.FindControl("lblAdsense")).Text = site.GetGoogleAdMarkup() + "<br />";
	}
}